package com.itheima.web.controller.sms;

import com.itheima.result.PageResult;
import com.itheima.result.ResultData;
import com.itheima.service.sms.SmsHomeNewProductService;
import com.itheima.vo.sms.SmsHomeNewProduct;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/newProduct")
public class SmsHomeNewProductController {
    @DubboReference
    private SmsHomeNewProductService smsHomeNewProductService;

    /**
     * 添加首页新品
     */
    @PostMapping("create")
    public ResultData create(@RequestBody SmsHomeNewProduct smsHomeNewProduct) {
        smsHomeNewProductService.save(smsHomeNewProduct);
        return ResultData.ok("添加成功", "");
    }

    /**
     * 批量删除首页新品
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public ResultData delete(@RequestParam("ids") List<Long> ids) {
        boolean res = smsHomeNewProductService.removeByIds(ids);
        if (res) {
            return ResultData.ok("删除成功", "");
        } else {
            return ResultData.error("删除失败");
        }
    }

    /**
     * 分页查询首页新品
     *
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @GetMapping("list")
    public ResultData list(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        PageResult pageResult = smsHomeNewProductService.listPage(productName, pageNum, pageSize, recommendStatus);
        return ResultData.ok("", pageResult);
    }

    /**
     * 批量修改首页新品状态
     */
    @PostMapping("update/recommendStatus")
    public ResultData updateRecommendStatus(@RequestParam("ids") List<Long> ids, @RequestParam("recommendStatus") Integer recommendStatus) {
        smsHomeNewProductService.updateRecommendStatus(ids, recommendStatus);
        return ResultData.ok("修改成功", "");
    }

    /**
     * 修改首页新品排序
     */
    @PostMapping("update/sort/{id}")
    public ResultData updateSort(@PathVariable("id") Long id_, @RequestParam("id") Long id, @RequestParam("sort") Integer sort) {
        smsHomeNewProductService.updateSort(id, sort);
        return ResultData.ok("修改成功", "");
    }
}
